<template>
  <div class="process-design-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>流程设计</span>
          <el-button type="primary" @click="handleCreate">新建流程</el-button>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="流程名称">
          <el-input v-model="searchForm.name" placeholder="请输入流程名称" clearable />
        </el-form-item>
        <el-form-item label="流程分类">
          <el-select v-model="searchForm.categoryId" placeholder="请选择分类" clearable>
            <el-option
              v-for="item in categoryOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 流程列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="流程名称" />
        <el-table-column prop="category" label="流程分类" />
        <el-table-column prop="key" label="流程标识" />
        <el-table-column prop="version" label="版本" width="80" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusLabel(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleDesign(row)">设计</el-button>
            <el-button type="success" link @click="handleDeploy(row)">部署</el-button>
            <el-button type="info" link @click="handleExport(row)">导出</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新建流程对话框 -->
    <el-dialog v-model="dialogVisible" title="新建流程" width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="流程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="流程分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择分类" class="w-full">
            <el-option
              v-for="item in categoryOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="流程标识" prop="key">
          <el-input v-model="form.key" placeholder="请输入流程标识">
            <template #append>.bpmn</template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'

const router = useRouter()

// 搜索表单
const searchForm = reactive({
  name: '',
  categoryId: '',
})

// 分类选项
const categoryOptions = ref([
  { id: 1, name: '人事流程' },
  { id: 2, name: '财务流程' },
  { id: 3, name: '行政流程' },
])

// 表格数据
const tableData = ref([
  {
    id: 1,
    name: '请假流程',
    category: '人事流程',
    key: 'leave',
    version: 1,
    status: 'draft',
    createTime: '2024-01-15 10:00:00',
  },
  // 更多数据...
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框控制
const dialogVisible = ref(false)
const formRef = ref<FormInstance>()

// 表单数据
const form = reactive({
  name: '',
  categoryId: '',
  key: '',
  remark: '',
})

// 表单校验规则
const rules = {
  name: [{ required: true, message: '请输入流程名称', trigger: 'blur' }],
  categoryId: [{ required: true, message: '请选择流程分类', trigger: 'change' }],
  key: [{ required: true, message: '请输入流程标识', trigger: 'blur' }],
}

// 获取状态标签类型
const getStatusType = (status: string) => {
  const map: Record<string, string> = {
    draft: 'info',
    deployed: 'success',
    invalid: 'danger',
  }
  return map[status] || 'info'
}

// 获取状态标签文本
const getStatusLabel = (status: string) => {
  const map: Record<string, string> = {
    draft: '草稿',
    deployed: '已部署',
    invalid: '已作废',
  }
  return map[status] || status
}

// 搜索
const handleSearch = () => {
  // 实现搜索逻辑
}

// 重置搜索
const resetSearch = () => {
  searchForm.name = ''
  searchForm.categoryId = ''
}

// 新建流程
const handleCreate = () => {
  dialogVisible.value = true
  // 重置表单
  form.name = ''
  form.categoryId = ''
  form.key = ''
  form.remark = ''
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // 跳转到流程设计器页面
      router.push({
        path: '/process/design/editor',
        query: {
          id: 'new',
          name: form.name,
          key: form.key,
          categoryId: form.categoryId,
        },
      })
      dialogVisible.value = false
    }
  })
}

// 设计流程
const handleDesign = (row: any) => {
  router.push({
    path: '/process/design/editor',
    query: {
      id: row.id,
      name: row.name,
      key: row.key,
    },
  })
}

// 部署流程
const handleDeploy = (row: any) => {
  ElMessageBox.confirm('确认部署该流程吗？', '提示', {
    type: 'warning',
  })
    .then(() => {
      // 实现部署逻辑
      ElMessage.success('部署成功')
    })
    .catch(() => {})
}

// 导出流程
const handleExport = (row: any) => {
  // 实现导出逻辑
}

// 删除流程
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确认删除该流程吗？', '提示', {
    type: 'warning',
  })
    .then(() => {
      // 实现删除逻辑
      ElMessage.success('删除成功')
    })
    .catch(() => {})
}

// 分页处理
const handleSizeChange = (val: number) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.process-design-list {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
